package android.mcsmtms.activity;

//import com.google.android.gms.maps.CameraUpdateFactory;
//import com.google.android.gms.maps.GoogleMap;
//import com.google.android.gms.maps.MapFragment;
//import com.google.android.gms.maps.model.LatLng;

import android.mcsmtms.R;
import android.mcsmtms.constant.GlobalConstants;
import android.mcsmtms.constant.GlobalConstants.UserTypes;
import android.mcsmtms.domain.User;
import android.mcsmtms.exception.UncaughtExceptionHandler;
import android.mcsmtms.fragment.NotificationsFragment;
import android.mcsmtms.fragment.HazardMapFragment;
import android.mcsmtms.fragment.MonitorSpeedFragment;
import android.mcsmtms.util.Utilities;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class McsmtmsActivity extends FragmentActivity {
	private HazardMapFragment hazardMapFragment;
	private MonitorSpeedFragment monitorSpeedFragment;
	private NotificationsFragment notificationsFragment;
	private int activeFragment = HAZARD_MAP_FRAGMENT_ID;
	public final static int HAZARD_MAP_FRAGMENT_ID = 0;
	private final static int MONITOR_SPEED_FRAGMENT_ID = 1;
	private final static int NOTIFICATIONS_FRAGMENT_ID = 2;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_mcsmtms);

		if (findViewById(R.id.container_content_fragment) != null) {

			// However, if we're being restored from a previous state,
			// then we don't need to do anything and should return or else
			// we could end up with overlapping fragments.
			if (savedInstanceState != null) {
				return;
			}
			openHazardMapFragment();
		}

		// Button aboutBtn = (Button) findViewById(R.id.about_btn);
		Button monitorSpeedBtn = (Button) findViewById(R.id.btn_monitor_speed);
		monitorSpeedBtn.setBackgroundResource(R.drawable.monitorspeed_btn_bg);
		monitorSpeedBtn.setOnClickListener(monitorSpeedBtnOnClickListener);

		Button hazardMapBtn = (Button) findViewById(R.id.btn_hazard_map);
		hazardMapBtn.setBackgroundResource(R.drawable.hazardmap_btn_bg);
		hazardMapBtn.setOnClickListener(hazardMapBtnOnClickListener);
		hazardMapBtn.setSelected(true);
		
		Button notificationsBtn = (Button) findViewById(R.id.btn_notifs);
		notificationsBtn.setBackgroundResource(R.drawable.notifs_btn_bg);
		notificationsBtn.setOnClickListener(notificationsBtnOnClickListener);
		
		User loggedUser = Utilities.getLoggedUser(this);
		
		if(!(UserTypes.valueOf(loggedUser.getUserType()) == UserTypes.SCOUT ||  
				UserTypes.valueOf(loggedUser.getUserType()) == UserTypes.BAYANAN_SCOUT ||
				UserTypes.valueOf(loggedUser.getUserType()) == UserTypes.ESANTOS_SCOUT ||
				UserTypes.valueOf(loggedUser.getUserType()) == UserTypes.JMOLINA_SCOUT ||
				UserTypes.valueOf(loggedUser.getUserType()) == UserTypes.JPRIZAL_SCOUT ||
				UserTypes.valueOf(loggedUser.getUserType()) == UserTypes.EROD_SCOUT ) ) {
			notificationsBtn.setVisibility(View.INVISIBLE);
		}
		
		Button refreshBtn = (Button) findViewById(R.id.btn_refresh);
		refreshBtn.setOnClickListener(refreshBtnOnClickListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.hazardmap_menu, menu);
//		menu.findItem(R.id.a_to_b).setChecked(true);
//		return true;
		
		//getMenuInflater().inflate(R.menu.send_report, menu);
		//return true;
		
		switch (activeFragment) {
			case HAZARD_MAP_FRAGMENT_ID:
				getMenuInflater().inflate(R.menu.hazardmap_menu, menu);
				menu.findItem(R.id.a_to_b).setChecked(true);
				break;
			case MONITOR_SPEED_FRAGMENT_ID:
				getMenuInflater().inflate(R.menu.send_report, menu);
				break;
			case NOTIFICATIONS_FRAGMENT_ID:
				getMenuInflater().inflate(R.menu.send_report, menu);
				break;
		}
		return true;
	}

//	@Override
//	public boolean onPrepareOptionsMenu(Menu menu) {
//		//super.onPrepareOptionsMenu(menu);
//		switch (activeFragment) {
//			case HAZARD_MAP_FRAGMENT_ID:
//				displayHazardMapOptionsMenu(menu, true);
//				break;
//			case MONITOR_SPEED_FRAGMENT_ID:
//				displayHazardMapOptionsMenu(menu, false);
//				break;
//			case NOTIFICATIONS_FRAGMENT_ID:
//				displayHazardMapOptionsMenu(menu, false);
//				break;
//		}
//		return super.onPrepareOptionsMenu(menu);
//	};
	
	private void displayHazardMapOptionsMenu(Menu menu, boolean isVisible) {
		menu.setGroupVisible(R.id.filter_group, isVisible);
		//menu.setVisible(R.id.direction, isVisible);
		MenuItem directionMenuItem = menu.findItem(R.id.direction);
		directionMenuItem.setVisible(isVisible);

		if (Build.VERSION.SDK_INT >= 11) {
			invalidateOptionsMenu();
		}
		
		//invalidateOptionsMenu();
//		 MenuInflater inflater = getMenuInflater();
//
//         if (isVisible)
//             inflater.inflate(R.menu.hazardmap_menu, menu);
//         else
//        	 inflater.inflate(R.menu.send_report, menu);
			
	}
	
	private View.OnClickListener monitorSpeedBtnOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if(activeFragment == MONITOR_SPEED_FRAGMENT_ID)
				return;
			
			displayRefreshBtn(false);
			openMonitorSpeedFragment();
			activeFragment = MONITOR_SPEED_FRAGMENT_ID;
			setSelectedButton(activeFragment);
		}
	};

	private View.OnClickListener hazardMapBtnOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if(activeFragment == HAZARD_MAP_FRAGMENT_ID)
				return;
			if (findViewById(R.id.container_content_fragment) != null) {
				displayRefreshBtn(true);
				openHazardMapFragment();
				activeFragment = HAZARD_MAP_FRAGMENT_ID;
				setSelectedButton(activeFragment);
			}
		}
	};

	private View.OnClickListener notificationsBtnOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if(activeFragment == NOTIFICATIONS_FRAGMENT_ID)
				return;
			
			displayRefreshBtn(true);
			openNotificationsFragment();
			activeFragment = NOTIFICATIONS_FRAGMENT_ID;
			setSelectedButton(activeFragment);
		}
	};
	
	private View.OnClickListener refreshBtnOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (activeFragment) {
				case HAZARD_MAP_FRAGMENT_ID:
					hazardMapFragment.refreshHazardMap();
					break;
				case MONITOR_SPEED_FRAGMENT_ID:
					
					break;
				case NOTIFICATIONS_FRAGMENT_ID:
					notificationsFragment.getNotifications();
					break;
			}
		}
	};

	private void openHazardMapFragment() {
		if (findViewById(R.id.container_content_fragment) != null) {
			if (hazardMapFragment == null) {
				// Create an instance of HazardMapFragment
				hazardMapFragment = new HazardMapFragment();

				// In case this activity was started with special instructions
				// from an Intent,
				// pass the Intent's extras to the fragment as arguments
				hazardMapFragment.setArguments(getIntent().getExtras());
			}

			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();

			// Add the fragment to the 'fragment_container' FrameLayout
			fragmentTransaction.replace(R.id.container_content_fragment,
					hazardMapFragment);
			fragmentTransaction.commit();
		}
	}

	private void openMonitorSpeedFragment() {
		if (findViewById(R.id.container_content_fragment) != null) {

			if (monitorSpeedFragment == null) {
				// Create an instance of ExampleFragment
				monitorSpeedFragment = new MonitorSpeedFragment();

				// In case this activity was started with special instructions
				// from an Intent,
				// pass the Intent's extras to the fragment as arguments
				monitorSpeedFragment.setArguments(getIntent().getExtras());
			}

			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();

			// Add the fragment to the 'fragment_container' FrameLayout
			// getSupportFragmentManager().beginTransaction()
			// .add(R.id.container_hazard_map, hazardMapFragment).commit();
			fragmentTransaction.replace(R.id.container_content_fragment,
					monitorSpeedFragment);
			fragmentTransaction.addToBackStack(null);
			fragmentTransaction.commit();
		}
	}

	private void openNotificationsFragment() {
		if (findViewById(R.id.container_content_fragment) != null) {
			if (notificationsFragment == null) {
				// Create an instance of ExampleFragment
				notificationsFragment = new NotificationsFragment();

				// In case this activity was started with special instructions
				// from an Intent,
				// pass the Intent's extras to the fragment as arguments
				notificationsFragment.setArguments(getIntent().getExtras());
			}

			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();

			// Add the fragment to the 'fragment_container' FrameLayout
			fragmentTransaction.replace(R.id.container_content_fragment,
					notificationsFragment);
			fragmentTransaction.commit();
		}
	}
	
	private void displayRefreshBtn(boolean isVisible) {
		Button refreshBtn = (Button) findViewById(R.id.btn_refresh);
		if(isVisible) {
			refreshBtn.setVisibility(View.VISIBLE);
		} else {
			refreshBtn.setVisibility(View.INVISIBLE);
		}
	}
	
	private void setSelectedButton(int activeFragment) {
		Button monitorSpeedBtn = (Button) findViewById(R.id.btn_monitor_speed);
		Button hazardMapBtn = (Button) findViewById(R.id.btn_hazard_map);
		Button notificationsBtn = (Button) findViewById(R.id.btn_notifs);
		
		switch (activeFragment) {
			case HAZARD_MAP_FRAGMENT_ID:
				hazardMapBtn.setSelected(true);
				monitorSpeedBtn.setSelected(false);
				notificationsBtn.setSelected(false);
				break;
			case MONITOR_SPEED_FRAGMENT_ID:
				hazardMapBtn.setSelected(false);
				monitorSpeedBtn.setSelected(true);
				notificationsBtn.setSelected(false);
				break;
			case NOTIFICATIONS_FRAGMENT_ID:
				hazardMapBtn.setSelected(false);
				monitorSpeedBtn.setSelected(false);
				notificationsBtn.setSelected(true);
				break;
		}
	}

//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		switch (item.getItemId()) {
//		case R.id.hospitals:
//			if (item.isChecked())
//				item.setChecked(false);
//			else
//				item.setChecked(true);
//			return true;
//		case R.id.police_stations:
//			if (item.isChecked())
//				item.setChecked(false);
//			else
//				item.setChecked(true);
//			return true;
//		case R.id.evac_centers:
//			if (item.isChecked())
//				item.setChecked(false);
//			else
//				item.setChecked(true);
//		case R.id.user_reports:
//			if (item.isChecked())
//				item.setChecked(false);
//			else
//				item.setChecked(true);
//			return true;
//		default:
//			return super.onOptionsItemSelected(item);
//		}
//	}

}
